# -*- encoding: utf-8 -*-
import time, datetime
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo import tools
# from odoo.addons import decimal_precision as dp
from odoo.tools import float_compare, float_round
from odoo.addons import base_cw


class account_cash_dialy_line_report(models.Model):
    _name = 'account.cash.dialy.line.report'
    _description = u'日记账明细查询'
    _auto = False

    name = fields.Char(u'序号', default=False)
    master_name = fields.Char(u'单据编号', default=False)
    account_id = fields.Many2one('cncw.account', u'会计科目')
    period_id = fields.Many2one('account.period', u'期别', required=False, )
    date = fields.Date(u'日期')
    origin = fields.Char(u'来源单据', default=False)
    sub_account_id = fields.Many2one('res.partner', u'辅助核算', required=False, )
    dc_type = fields.Selection([('D', u'收入'), ('C', u'支出')], u'收支', default='D')
    debit = fields.Float(u'收入', digits='Amount')
    credit = fields.Float(u'支出', digits='Amount')
    lc_debit = fields.Float(u'收入（本币）', digits='Amount')
    lc_credit = fields.Float(u'支出（本币）', digits='Amount')
    remark = fields.Char(u'摘要', default=False)
    balance = fields.Float(u'余额', digits='Amount')

    # 获取借款单查询明细
    def init(self):
        tools.drop_view_if_exists(self._cr, 'account_cash_dialy_line_report')
        self._cr.execute("""
             create or replace view account_cash_dialy_line_report as (
                    select a.id,a.sequence as name,b.name as master_name,
                    b.account_id,b.period_id,b.date,
                    a.origin,a.sub_account_id,a.dc_type,a,debit,a.credit,
                    a.lc_debit,a.lc_credit,
                    a.name as remark,a.balance

                    from account_cash_dialy_line a
                    left join account_cash_dialy b on b.id=a.master_id
                    where b.state='audited'
             )""")
